package com.example.cinema.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.databinding.DataBinderMapper;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.databinding.library.baseAdapters.BR;
import androidx.recyclerview.widget.RecyclerView;

import com.example.cinema.R;
import com.example.cinema.drawview.CircleVideoPlayer;

import java.util.List;

public class PostsRecyclerAdapter<T> extends RecyclerView.Adapter<ItemHandle> {

    private int Layoutid;
    private List<T> dataliest;
    private Context context;
    public PostsRecyclerAdapter(int layoutid, List<T> dataliest, Context context) {
        Layoutid = layoutid;
        this.dataliest = dataliest;
        this.context = context;
    }

    @NonNull
    @Override
    public ItemHandle onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewDataBinding VDB = DataBindingUtil.inflate(LayoutInflater.from(context),Layoutid,parent,false);
        ItemHandle handle = new ItemHandle(VDB);
        return handle;
    }

    @Override
    public void onBindViewHolder(@NonNull ItemHandle holder, int position) {

        holder.setIsRecyclable(true);
        DataBindingUtil.bind(holder.itemView).setVariable(BR.data,dataliest.get(position));

        //观看完整块点击
        holder.itemView.findViewById(R.id.comment).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(stopListener != null)
                {
                    stopListener.PlayerStop();
                }
            }
        });
        //点赞
        holder.itemView.findViewById(R.id.dianzan).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("ZZZ","点赞");
                ImageView image = v.findViewById(R.id.zanimg);
                image.getDrawable().setLevel(2);
            }
        });
    }
    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

    @Override
    public int getItemCount() {
        return dataliest.size();
    }

    //关闭播放
    public interface OnPlayerStopListener{
        void PlayerStop();
    }
    //外部调用
    private OnPlayerStopListener stopListener;
    public void  setOnPlayerStopListener(OnPlayerStopListener listener){
        stopListener = listener;
    }
}
class ItemHandle extends RecyclerView.ViewHolder{
//    public ViewDataBinding dataBinding;
    public ItemHandle(@NonNull ViewDataBinding itemView) {
        super(itemView.getRoot());
//        dataBinding = itemView;
    }

}